//
//  RW_UserModel.m
//  SleepApp
//
//  Created by MAC on 2021/3/25.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_UserModel.h"
#import "AppDelegate.h"

@implementation RW_UserModel
MJCodingImplementation
-(NSArray *)ignoredKeyList{
    return @[
        @"isLogin"
    ];
}
-(BOOL)isLogin{
    BOOL isHave = self.token.length;
    return isHave;
}

#pragma mark -- 网络请求
/**
 注册
 account:账号
 password:密码
 passwordConfirm:确认密码
 nickName:昵称
 code:验证码
 */
+ (FBLPromise<id> *)registrParamsMap:(NSDictionary *)paramsMap{
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool putWithUrl:registrUrl refreshCache:NO params:paramsMap success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            fulfill(@"");
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}

/**
 登陆
 account:账号
 password:密码
 code:验证码
 */
+ (FBLPromise<RW_UserModel *> *)loginParamsMap:(NSDictionary *)paramsMap {
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool postWithUrl:loginUrl refreshCache:NO params:paramsMap success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            RW_UserModel *m = [RW_UserModel yy_modelWithJSON:model.data];
            [RW_UserModel updataModel:m];
            fulfill(m);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}

/** 获取用户信息 */
+ (FBLPromise<RW_UserModel *> *)getUserInfo {
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool getWithUrl:getUserInfoUrl refreshCache:NO params:nil success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            RW_UserModel *m = [RW_UserModel yy_modelWithJSON:model.data];
            [RW_UserModel updataModel:m];
            fulfill(m);
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}

/** 刷新token */
+ (FBLPromise<NSString *> *)checkToken {
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool postWithUrl:checkTokenUrl refreshCache:NO params:nil success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                [RW_UserModel removeModel];
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            fulfill(@"");
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}

/** 退出登录 */
+ (FBLPromise<id> *)loginOut {
    return [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
        [HTTPRequestTool postWithUrl:loginOutUrl refreshCache:NO params:nil success:^(id response) {
            HTTPLogicalModel *model = [HTTPLogicalModel yy_modelWithJSON:response];
            if (!model.success){
                reject([NSError errorWithDomain:model.message code:model.code userInfo:nil]);
                return;
            }
            [RW_UserModel removeModel];
            AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
            [appDelegate reloadWindows];
            fulfill(@"");
        } fail:^(NSError *error) {
            reject(error);
        }];
    }];
}
@end
